package com.yukexx.modules.user.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yukexx.common.utils.DateUtis;
import com.yukexx.common.utils.Ftpc;
import com.yukexx.common.utils.IDUtils;
import com.yukexx.common.utils.Page;
import com.yukexx.common.utils.YkResult;
import com.yukexx.modules.user.entity.User;
import com.yukexx.modules.user.entity.UserQuery;
import com.yukexx.modules.user.service.UserStudentService;

/**
 * <p>Title: UserController</p>
 * <p>Description: 用户控制层</p>
 * <p>Company: www.yukexx.com</p>
 * @author  王磊
 * @date	2017年12月21日17:54:05
 * @version 1.0
 */
@Controller
@RequestMapping("user")
public class UserController { 
	
	@Autowired
	private UserStudentService studentService;
	
	
	/**
	 * <p>Title: insert</p>
	 * <p>Description: 测试插入用户数据</p>
	 * <p>Company: www.yukexx.com</p>
	 * @author  王磊
	 * @date	2017年12月22日11:08:56
	 * @version 1.0
	 */
	@RequestMapping("get")
	@ResponseBody
	public List<User> get(UserQuery example) {
		return studentService.get(example);
	}
	
	/**
	 * <p>Title: login</p>
	 * <p>Description:用户登录 </p>
	 * @param user
	 * @return
	 */
	@RequestMapping("login")
	@ResponseBody
	public YkResult login(User user,HttpServletRequest request,HttpServletResponse response){
		Subject subject = SecurityUtils.getSubject();
		if(!subject.isAuthenticated()){
			UsernamePasswordToken token = new UsernamePasswordToken(user.getName(),user.getPassword());
			try{
				subject.login(token);
			}catch(AuthenticationException ae){
				return YkResult.build(300, "用户名或密码错误");
			}
		}
		return YkResult.ok();
	}
}
